package com.yangxk.kadmin.auth.dao;

import com.yangxk.kadmin.auth.beans.TableBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class DbDao {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private TableBean tableBean;

    public Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException("获取数据库连接失败");
        }
    }

    public void close(ResultSet rs, Statement statement, Connection connection) {
        try {
            rs.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public List<String> findUserAuthIds(String userId) {
        String sql = "select " + tableBean.getRoleAuth().getAuthIdName()
                        + " from " + tableBean.getRoleAuth().getTableName()
                        + " WHERE " + tableBean.getRoleAuth().getRoleIdName()
                        + " in (SELECT " + tableBean.getUserRole().getRoleIdName()
                        + " from " + tableBean.getUserRole().getTableName()
                        + " where " + tableBean.getUserRole().getUserIdName() + " = " + userId + ")";

        Connection connection = getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            List<String> res = new ArrayList<>();
            while (rs.next()) {
                String auth_id = rs.getString(tableBean.getRoleAuth().getAuthIdName());
                res.add(auth_id);
            }
            close(rs, statement, connection);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询用户权限ID出错");
        }

    }

    public Map<String, String> findAllAuth() {
        String sql = "select " + tableBean.getAuth().getIdName()
                        + ", "
                        + tableBean.getAuth().getUrlName()
                        + " from " + tableBean.getAuth().getTableName();

        if (tableBean.getAuth().isUseStatus()) {
            sql += " where " + tableBean.getAuth().getStatusWhere();
        }

        Connection connection = getConnection();
        Map<String, String> res = new HashMap<>();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                String id = rs.getString(tableBean.getAuth().getIdName());
                String url = rs.getString(tableBean.getAuth().getUrlName());
                res.put(url, id);
            }
            close(rs, statement, connection);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询所有权限出错");
        }
    }
}
